package cn.itcast.bos.service.take_delivery.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillElasticsearchRepository;
import cn.itcast.bos.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
	@Autowired
	private WayBillRepository wayBillRepository;
	@Autowired
	private WayBillElasticsearchRepository wayBillElasticsearchRepository;

	@Override
	public void save(WayBill model) throws Exception {
		// 保證运单号唯一
		WayBill persistWayBill = wayBillRepository.findByWayBillNum(model.getWayBillNum());

		if (persistWayBill == null) {
			// 运单不存在
			wayBillRepository.save(model);
			wayBillElasticsearchRepository.save(model);
		} else {
			// 运单已经存在
			Integer id = persistWayBill.getId();// 获取持久运单的Id
			BeanUtils.copyProperties(persistWayBill, model);// 把要保存的对象复制到持久对象中
			// 持久对象设置Id(保存的运单对象中没有id属性)
			persistWayBill.setId(id);
			wayBillElasticsearchRepository.save(persistWayBill);
		}
	}

	@Override
	public Page<WayBill> findAll(WayBill wayBill, Pageable pageable) {
		// 无条件查询
		if (StringUtils.isBlank(wayBill.getWayBillNum()) && StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress()) && StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)){
			return wayBillRepository.findAll(pageable);
		}else{
			//添加组合查询条件对象
			BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
			//运单号查询条件
			if(StringUtils.isNotBlank(wayBill.getWayBillNum())){
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				boolQueryBuilder.must(termQueryBuilder);
			}
			//发货地址查询条件
			if(StringUtils.isNotBlank(wayBill.getSendAddress())){
				//1.单个词条完全匹配
				WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				//2.多个词条组合，分别查询后取交集
				QueryStringQueryBuilder defaultOperator = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddress").defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
				boolQueryBuilder2.should(wildcardQueryBuilder);
				boolQueryBuilder2.should(defaultOperator);
				
				boolQueryBuilder.must(boolQueryBuilder2);
			}
			//收货地址条件查询
			if(StringUtils.isNotBlank(wayBill.getRecAddress())){
				//1.单个词条完全匹配
				WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				//2.多个词条组合，分别查询后取交集
				QueryStringQueryBuilder defaultOperator = new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress").defaultOperator(Operator.AND);
				
				BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
				boolQueryBuilder2.should(wildcardQueryBuilder);
				boolQueryBuilder2.should(defaultOperator);
				
				boolQueryBuilder.must(boolQueryBuilder2);
			}
			//发货类型查询(当日到达、次日到达)
			if(StringUtils.isNotBlank(wayBill.getSendProNum())){
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				boolQueryBuilder.must(termQueryBuilder);
			}
			//货物状态查询条件(0,1,2,3,4)
			if(wayBill.getSignStatus()!=null && wayBill.getSignStatus()!=0){
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
				boolQueryBuilder.must(termQueryBuilder);
			}
			SearchQuery searchQuery = new NativeSearchQuery(boolQueryBuilder);
			searchQuery.setPageable(pageable);
			//索引库查询
			return wayBillElasticsearchRepository.search(searchQuery);
		}
	}

	@Override
	public WayBill findByOrderNum(String wayBillNum) {
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}
}
